package com.edais.service.imp;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.edais.dao.ChartDao;
import com.edais.service.ChartService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;

@Service

public class ChartServiceImpl implements ChartService{
	private static final Log logger=LogFactory.getLog(ChartServiceImpl.class);
	@Autowired
	private ChartDao chartdao;	
	//查年利率
	@Override
	public List<Map<String, Object>> findYieByUserId(Map<String, Object> parmMap)
			throws Exception {
	
		return chartdao.findYieByUserId(parmMap);
	}
	
	//查本金和收益
	@Override
	public String getmoneyUserId(Map<String, Object> parmMap) throws Exception {

		String transCode = "E360016";
		parmMap.put("userId", parmMap.get("userId"));
		parmMap.put("dayNum", parmMap.get("dayNum"));
		String dayNum=(String)parmMap.get("dayNum");
		if(dayNum==null||StringUtils.isBlank(dayNum)){
			dayNum="7";
		}
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();//查询结果一个json的字符串	
		Map<String, Object> resultMap = new HashMap<String, Object>();//查询结果一个json的字符串	
		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, null, parmMap);
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);	
		if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
				Map<String, Object> amountMap=null;
				//
				Map<String,Map<String, Object>> map=null;
				Calendar cal=Calendar.getInstance();	
				java.text.SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
				map=new TreeMap<String,Map<String, Object>>();//使用TreeMap用作排序,默认升序
				for (int i = 0; i < Integer.parseInt(dayNum); i++) {
					amountMap=new HashMap<String,Object>();
					amountMap.put("investAmount", "0.00");
					amountMap.put("incomeAmount", "0.00");
					amountMap.put("totalRise", "0.00");
					cal.setTime(new Date());
					cal.add(Calendar.DATE,-(i+1));
					Date date1=cal.getTime();			
					String sss1=sim.format(date1);
					amountMap.put("incomeDay", sss1);					
					map.put(sss1, amountMap);
				}
				//System.out.println("========="+map.size());
				if(jsonArr != null && jsonArr.size() > 0){
					for(int i = 0; i < jsonArr.size(); i++){
						JSONObject jsonObj = jsonArr.getJSONObject(i);
						amountMap=new HashMap<String,Object>();
						amountMap.put("investAmount", jsonObj.getString("investAmount"));
						amountMap.put("incomeAmount", jsonObj.getString("incomeAmount"));
						amountMap.put("totalRise",jsonObj.getString("totalRise"));
						amountMap.put("incomeDay", jsonObj.getString("incomeDay"));
						map.put(jsonObj.getString("incomeDay"), amountMap);
					}
				}
				//System.out.println("========="+jsonArr.size());
				for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
					String key = (String) iterator.next();
					resultList.add(map.get(key));
				}
	
				resultMap.put("listSize", dayNum);
				resultMap.put("list", resultList);

		} else {
			logger.debug("=====E360016核心接口调用异常=====");
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			resultMap.put("listSize", 0);
			resultMap.put("list", new ArrayList());
		}
		// 设置返回code
		return CommonUtil.setResultStringCn(resultMap, Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE,"");
	}

	
		/**
		 * 
		 * 
		 * 算历史本金的
		 */
		@Override
		public String queryAmountStatementList(Map<String, Object> map)
				throws Exception {
			
			 // 调用核心接口E360016
			String transCode = "E360016";
			Map<String, Object> parmMap = new HashMap<String, Object>();
			parmMap.put("userId", map.get("userId"));
			parmMap.put("dayNum", "1");//随便给个值
			parmMap.put("flag", map.get("flag"));
			String pageSize= (String) map.get(Consts.Page.PAGE_SIZE_NAME);//页码
			String current= (String) map.get(Consts.Page.PAGE_CURRENT_NAME);//每页行数
			if(pageSize==null||StringUtils.isBlank(pageSize)||"0".equals(pageSize)){
				pageSize="10";
			}
			if(current==null||StringUtils.isBlank(current)||"0".equals(current)){
				current="1";
			}
			int beginNum=(Integer.parseInt(current)-1)*Integer.parseInt(pageSize)+1;
			int endNum=Integer.parseInt(pageSize)*Integer.parseInt(current);
			
			parmMap.put("rowBegin", String.valueOf(beginNum));
			parmMap.put("rowEnd", String.valueOf(endNum));
			String jsonStr = CommonUtil.constructJsonStr(transCode, map.get("userId").toString(),
					parmMap);
			String rtnStr = CommonUtil.getCoreValue(jsonStr);//请求核心
			//返回数据JOSN格式
			List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();//查询结果一个json的字符串	
			Map<String, Object> resultMap = new HashMap<String, Object>();//查询结果一个json的字符串	
			JSONObject jsonObject = JSONObject.fromObject(rtnStr);
			if(Consts.SUCCESS_CODE.equals(jsonObject.get("rescode"))){
				JSONArray jsonArr = jsonObject.getJSONArray("list");
				if(jsonArr != null && jsonArr.size() > 0){
					Map<String, Object> amountMap=null;
					for(int i = 0; i < jsonArr.size(); i++){
						JSONObject jsonObj = jsonArr.getJSONObject(i);
						amountMap=new HashMap<String,Object>();
						amountMap.put("investAmount", jsonObj.getString("investAmount"));
						amountMap.put("incomeAmount", jsonObj.getString("incomeAmount"));
						amountMap.put("totalRise", jsonObj.getString("totalRise"));
						amountMap.put("incomeDay", jsonObj.getString("incomeDay"));
						amountMap.put("addrateRise", jsonObj.getString("addrateRise"));
						resultList.add(amountMap);
					}
										
					resultMap.put("listSize", jsonObject.getString("listSize"));
					resultMap.put("totalInvestAmount", jsonObject.getString("totalInvestAmount"));
					resultMap.put("totalIncomeAmount", jsonObject.getString("totalIncomeAmount"));
					resultMap.put("totalNum", jsonObject.getString("totalNum"));
					resultMap.put("list", resultList);
				}else{
					logger.info("=====E360016核心接口查询为空=====");
					resultMap.put("listSize", 0);
					resultMap.put("totalInvestAmount", 0);
					resultMap.put("totalNum", 0);
					resultMap.put("list", new ArrayList());
				}

			} else {
				logger.debug("=====E360016核心接口调用异常=====");
				logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
				resultMap.put("listSize", 0);
				resultMap.put("totalInvestAmount", 0);
				resultMap.put("totalTncomeAmount", 0);
				resultMap.put("totalNum", 0);
				resultMap.put("list", new ArrayList());
			}
			// 设置返回code
			return CommonUtil.setResultStringCn(resultMap, Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE,"");
		}


	}